package com.cyzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyzy.dto.Result;
import com.cyzy.mapper.ArtGalleryMapper;
import com.cyzy.mapper.ArtWorksMapper;
import com.cyzy.pojo.LikeRecord;
import com.cyzy.vo.CampusWorks;
import com.cyzy.pojo.CourseStu;
import com.cyzy.service.ArtGalleryService;
import com.cyzy.vo.ChildAlbum;
import com.cyzy.vo.UserAlbum;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ArtGalleryServiceImpl extends ServiceImpl<ArtGalleryMapper, LikeRecord> implements ArtGalleryService {

    @Autowired
    private ArtGalleryMapper artGalleryMapper;

    @Autowired
    private ArtWorksMapper artWorksMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<CampusWorks> getArtworkInfoListByTeacher(Long campusId,Long teacherId,Long parentId) {
        if (teacherId!=0&&parentId==0){
            //老师查询
            List<CampusWorks> artworkInfoList = artGalleryMapper.getArtworkInfoListByTeacher(campusId,teacherId);
            System.out.println("作品列表为："+artworkInfoList);
            return artworkInfoList;
        }else if (parentId!=0&&teacherId==0){
            //家长查询
            List<CampusWorks> artworkInfoList = artGalleryMapper.getArtworkInfoListByParent(campusId,parentId);
            System.out.println("作品列表为："+artworkInfoList);
            return artworkInfoList;
        }else{
            return List.of();
        }


    }

    @Override
    public List<ChildAlbum> queryChildAlbum(Long parentId,Long teacherId) {
        if (parentId!=0&&teacherId==0){
            System.out.println("家长查询自己的孩子");
            return artGalleryMapper.queryChildAlbumByParent(parentId);
        } else if (parentId==0&&teacherId!=0) {
            System.out.println("老师查询所属班级学生");
            return artGalleryMapper.queryChildAlbumByTeacher(teacherId);
        }else {
            return List.of();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean UserChangeLikes(LikeRecord likeRecord) {//true是点赞成功，false是点赞失败

        try {
            //查询该作品的作者ID
            likeRecord.setStuId(artWorksMapper.selectById(likeRecord.getWorkId()).getStuId());

            //投票流程--->1.先判断用户是否第一次投票，是第一次投票则新增，不是则修改投票状态
            if (likeRecord.getTeacherId()==0&&likeRecord.getParentId()!=0){
                //家长投票

                return this.userThumbsUpMethod("parentId",likeRecord,likeRecord.getParentId());

            }else if (likeRecord.getTeacherId()!=0&&likeRecord.getParentId()==0){
                //老师投票
                return this.userThumbsUpMethod("teacherId",likeRecord,likeRecord.getTeacherId());
            }else {
                System.out.println("投票出错");
                return false;
            }
        }catch (Exception e){
            throw  e;
        }
    }

    @Override
    public List<UserAlbum> selectUserAlbumList(Long stuId) {
        return artGalleryMapper.selectUserAlbumList(stuId);
    }

    //用户点赞的方法
    private Boolean userThumbsUpMethod(String str,LikeRecord likeRecord,Long id){

        QueryWrapper<LikeRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("campusId",likeRecord.getCampusId())
                .eq(str,id)
                .eq("workId",likeRecord.getWorkId());
        LikeRecord record = artGalleryMapper.selectOne(wrapper);
        System.out.println("这是查询出来的点赞记录:"+record);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        likeRecord.setLikeTime(dateFormat.format(date));
        if (record!=null){
            //非第一次投票
            String status;
            if (record.getLikeStatus().equals("true")){
                System.out.println("用户取消点赞");
                status="false";
            }else{
                status="true";
            }
            UpdateWrapper<LikeRecord> queryWrapper = new UpdateWrapper<>();
            queryWrapper.set("likeTime",likeRecord.getLikeTime())
                    .set("likeStatus",status)
                    .eq("likeId",record.getLikeId());
            if (artGalleryMapper.update(null, queryWrapper)>0){
                return true;
            }else {
                return false;
            }
        }else{
            //第一次投票
            likeRecord.setLikeStatus("true");
            int i = artGalleryMapper.insert(likeRecord);
            if (i!=0){
                return true;
            }else{
                return false;
            }

        }
    }
}
